﻿using Dm.util;
using ServiceCore.DbContext;
using ServiceCore.Entity.Arg;
using ServiceCore.Entity.Const.Record;
using ServiceCore.Entity.Const.Site;
using ServiceCore.Entity.Model;
using ServiceCore.Utility.Extension;

namespace ServiceCore.Service
{
    /// <summary>
    /// 封装处理
    /// </summary>
    public partial class TextParseService : SqlsugarServices<RecordInfo>
    {
        public const string NoTakeName = "不支持的解析内容";

        /// <summary>
        /// 封装处理对象
        /// </summary>
        /// <param name="arg"></param>
        public void Text_Parse_Datas_Init(ArgTextParse arg)
        {
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return;
            if (arg.Lines.Any() == false)
                return;

            var ServiceType = string.Empty;
            var Source = string.Empty;
            string tagStr = string.Empty;
            arg.Lines.ForEach(x =>
            {
                ServiceType = SiteServiceType.SiteServiceType_Macao;
                Source = x;
                if (x.Contains("香港"))
                {
                    ServiceType = SiteServiceType.SiteServiceType_HongKong;
                    Source = Source.Replace("香港", "");
                }
                else if (x.Contains("港"))
                {
                    ServiceType = SiteServiceType.SiteServiceType_HongKong;
                    Source = Source.Replace("港", "");
                }
                else if (x.Contains("澳"))
                {
                    ServiceType = SiteServiceType.SiteServiceType_Macao;
                    Source = Source.Replace("澳", "");
                }

                tagStr = x;
                tagStr = tagStr.GetTxtReplaceNumeric().Replace(",", "");
                arg.Datas.add(new TextParseDto()
                {
                    Source = x,
                    SourceProcess = Source,
                    ServiceType = ServiceType,
                    SiteId = arg.SiteId,
                    IsNumberLine = tagStr.IsAllNumeric(),
                    IsTotalLine = Source.Contains("共")
                });
            });
        }

        

        /// <summary>
        /// 不支持
        /// </summary>
        /// <param name="arg"></param>
        public void Text_Parse_Datas_NoTake(ArgTextParse arg)
        {
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return;
            if (arg.Datas.Any() == false)
                return;

            var lstNoTake = new List<string>() { "三连", "三中三", "二连", "二中二", "3连", "2连", "3中3", "2中2", "平特", "平肖", "平尾" };
            arg.Datas.ForEach(x =>
            {
                if (lstNoTake.Any(n => x.SourceProcess.contains(n)))
                {
                    x.Result = ParsemResult.ParsemResult_Fail;
                    x.ResultTip = NoTakeName;
                }
            });
        }

        /// <summary>
        /// 处理模式分组
        /// </summary>
        /// <param name="arg"></param>
        public void Text_Parse_Datas_ParsemMode(ArgTextParse arg)
        {
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return;
            if (arg.Datas.Any() == false)
                return;

            int idx = -1, lastIdx = -1;
            foreach (var item in arg.Datas.Where(x => x.Result != ParsemResult.ParsemResult_Fail).ToList())
            {
                //特殊处理
                if (item.SourceProcess.Contains(",") && item.SourceProcess.IndexOf(",") == 0)
                    item.SourceProcess = item.SourceProcess.Substring(1); //处理第一个字符,号，前面没有任何内容

                if (arg.lstTagTypeName.Any(n => item.SourceProcess.Contains(n)))
                {
                    //标签类型
                    item.ParsemMode = ParsemMode.ParsemMode_TagType;
                    if (item.SourceProcess.Contains("包"))
                        item.ParsemChildMode = ParsemChildMode.ParsemChildMode_TagType_Pack;
                    else if (item.SourceProcess.Contains("各"))
                        item.ParsemChildMode = ParsemChildMode.ParsemChildMode_TagType_Each;
                    else
                    {
                        //虎:20，龙:16，兔:40，共:76
                        idx = item.SourceProcess.IndexOf(":");
                        lastIdx = item.SourceProcess.LastIndexOf(":");
                        if (idx > -1 && idx != lastIdx 
                            && arg.lstTagTypeName.Contains(item.SourceProcess.Substring(idx - 1, 1)) 
                            && item.SourceProcess.Substring(idx + 1, 1).IsAllNumeric())
                        {
                            item.SourceProcess = item.SourceProcess.Replace(":", "包");
                            item.ParsemChildMode = ParsemChildMode.ParsemChildMode_TagType_Pack;
                            continue;
                        }

                        item.ParsemChildMode = ParsemChildMode.ParsemChildMode_TagType_General;
                    }
                }
                else
                {
                    //标签
                    item.ParsemMode = ParsemMode.ParsemMode_Tag;
                    if (item.SourceProcess.Contains("各"))
                        item.ParsemChildMode = ParsemChildMode.ParsemChildMode_Tag_Each;
                    else
                        item.ParsemChildMode = ParsemChildMode.ParsemChildMode_Tag_General;
                }

                //合计
                if (item.SourceProcess.Contains("共"))
                    item.Total = NumberExtractor.ExtractNumberAfterGong(item.SourceProcess);
            }
        }


        ///// <summary>
        ///// 合并行
        ///// </summary>
        ///// <param name="arg"></param>
        //public void Text_Parse_Datas_MergeLine(ArgTextParse arg)
        //{
        //    /*
        //        12-13-17+16+14
        //        22-23-27-26-24-
        //        32-33-37-36-34-
        //        42-43-47-46-44一个数各10块共200总计200+50+200+120+200+25+200=995
        //     */
        //    if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
        //        return;
        //    if (arg.Datas.Any() == false)
        //        return;

        //    Dictionary<int, int> mergeDic = new Dictionary<int, int>();
        //    var idx = -1;
        //    for (int i = arg.Datas.Count - 1; i >= 1; i--)
        //    {
        //        if (arg.Datas[i].IsTotalLine)
        //        {
        //            idx = i;
        //            while (idx >= 0)
        //            {
        //                idx = i - 1;
        //                if (arg.Datas[idx].IsNumberLine == false)
        //                    break;
        //            }
        //            if (i - idx >= 2)
        //            {
        //                mergeDic.Add(i, idx);
        //            }
        //        }
        //    }
        //    if (mergeDic.Any() == false)
        //        return;

        //    var temp = string.Empty;
        //    foreach (var key in mergeDic.Keys)
        //    {
        //        temp = string.Empty;
        //        for (int i = key; i > idx; i--)
        //        {
        //            temp = arg.Datas[i] + temp;
        //            arg.Datas.RemoveAt(i);
        //        }
        //    }
        //}
    }
}
